<?php
namespace jy_newpingfee;
defined('BY_JYA') or exit('error');
/*
	团队长分红计算
	
*/
class commission extends \plugin{
	public function up_commission1(){
		$list=$this->getall('commission',array('status'=>0,'mid'=>$_SESSION['uid']));
		foreach($list as $item){
			$order=pdo_get('jy_weishop_order',array('id'=>$item['orderid'],'uniacid'=>$_SESSION['uniacid']));
			if($order['status']==3 || $order['status']==6){
				$this->order_complete($order);
			}
		}
	}
	/*
		订单完成时，把分红状态由初始的订单待完成升级为待结算
	*/
	public function order_complete($order){
		$this->update('commission',array('status'=>1,'complete_time'=>TIMESTAMP),array('status'=>0,'orderid'=>$order['id']));
	}
	/*
		创建结算单
	*/
	public function create_balance($mid,$post=array()){
		if(!$mid){
			exi('请先选择要结算的会员！','error');
		}
		if($post['pay_type']=='alipay'){
			if(!$post['realname']){
				exi('请先填写真实姓名！','error');
			}
			if(!$post['alipay']){
				exi('请先填写支付宝帐号！','error');
			}
			if(!$post['alipay1']){
				exi('请确认帐号','error');
			}
			if($post['alipay1']!=$post['alipay']){
				exi('确认帐号与支付宝帐号不匹配！','error');
			}
		}
		if($post['pay_type']=='union'){
			if(!$post['realname']){
				exi('请输入姓名','error');
			}
			if(!$post['bankname']){
				exi('请输入所属银行','error');
			}
			if(!$post['bankcard']){
				exi('请银行卡号','error');
			}
			if(!$post['bankcard1']){
				exi('请确认卡号','error');
			}
			if($post['bankcard']!=$post['bankcard1']){
				exi('确认卡号与银行卡号不匹配！','error');
			}
		}
		//获取可结算分红
		$params=array('uniacid'=>$_SESSION['uniacid'],'mid'=>$mid,'status'=>1);
		if($this->fenxiaoset['balance_day']){
			//结算天数
			$time=TIMESTAMP-intval($this->fenxiaoset['balance_day'])*86400;
			$params['complete_time <']=$time;
		}
		$money=pdo_getsum('jy_newpingfee_commission',$params,'money');
		if($money<=0){
			exi('没有可结算的分红！','error');
		}
		$commission_num=$this->count('commission',$params,'id');
		//结算最低分红检测
		if($money<$this->fenxiaoset['balance_fee']){
			exi('满￥'.$this->fenxiaoset['balance_fee'].'才可结算！','error');
		}
		$downmids=pdo_getall('jy_newpingfee_member',array('fromuser'=>$mid,'uniacid'=>$_SESSION['uniacid']),'mid','mid');
		$canyusnum=0;
		if($downmids){
			$downmids=array_keys($downmids);
			$canyus=pdo_getnorepeat('jy_newpingfee_team_log',array('mid'=>$downmids,'uniacid'=>$_SESSION['uniacid']),'mid');
			$canyusnum=count($canyus);
		}
		if($canyusnum<$this->fenxiaoset['tui_userbuy'] && $this->fenxiaoset['tui_userbuy']){
			exi('您的直推下线，满'.$this->fenxiaoset['tui_userbuy'].'人参与，才可结算！','error');
		}
		$new_balance=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$mid,
			'balancesn'=>get_ordersn($this->tablename('commission'),'balancesn','AC'),
			'commission_num'=>$commission_num,
			'money'=>$money,
			'pay_type'=>$post['pay_type'],
			'realname'=>$post['realname'],
			'alipay'=>$post['alipay'],
			'bankname'=>$post['bankname'],
			'bankcard'=>$post['bankcard'],
			'createtime'=>TIMESTAMP,
			'status'=>0,
		);
		//结算手续费
		if($this->fenxiaoset['balance_per'] && $post['pay_type']!='credit'){
			$new_balance['balance_pay']=intval($new_balance['money']*$this->fenxiaoset['balance_per'])/100;
		}else{
			$new_balance['balance_pay']=0;
		}
		$new_balance['final_money']=$new_balance['money']-$new_balance['balance_pay'];
		$this->insert('balance',$new_balance);
		$balance_id=pdo_insertid();
		$update=array(
				'status'=>2,
				'balancesn'=>$new_balance['balancesn'],
		);
		$this->update('commission',$update,$params);
		if($balance_id && $post['pay_type']=='credit'){
			$this->check_balance($balance_id,1);
			$this->pay_balance($balance_id,'credit');
		}
		return $balance_id;
	}
	/*
		审核结算单
	*/
	public function check_balance($balance_id,$status,$remark=''){
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
			'remark'=>$remark,
		);
		$this->update('balance',$update,array('id'=>$balance_id));
	}
	/*
		打款结算单
	*/
	public function pay_balance($balance_id,$pay_type){
		$balance=$this->get('balance',array('id'=>$balance_id,'uniacid'=>$_SESSION['uniacid']));
		if(!$balance || $balance['status']!=1){
			exi('结算单不存在或者不符合打款条件，打款失败！','error');
		}
		$pay_types=array_keys($this->pay_types);
		if(!$pay_type || !in_array($pay_type,$pay_types)){
			exi('未设置打款方式或者打款方式不合法，打款失败！','error');
		}
		$func_name='pay_balance_by_'.$pay_type;
		$trade_no=$this->$func_name($balance['mid'],$balance['final_money'],$balance['balancesn']);
		$update=array(
			'status'=>3,
			'pay_type'=>$pay_type,
			'pay_trade_no'=>$trade_no,
			'pay_time'=>TIMESTAMP,
		);
		$this->update('balance',$update,array('id'=>$balance['id']));
		return true;
	}
	/*
		打款到余额
	*/
	protected function pay_balance_by_credit($mid,$money,$balancesn){
		return cfc('core')->member_credit($mid,'credit2',$money,true,$this->plugin_name,$remark='分销分红打款');
	}
	/*
		打款到微信零钱
	*/
	protected function pay_balance_by_wechat($mid,$money,$balancesn){
		$params=array(
			'openid'=>$mid,
			'mch_billno'=>$balancesn,
			'money'=>$money,
			'remark'=>'代理商分红发放',
		);
		$res=cfc('wechat_pay')->mch_pay($params);
		return $res['payment_no'];
	}
	/*
		打款到支付宝
	*/
	protected function pay_balance_by_alipay($mid,$money,$balancesn){
		exi('暂不支持打款到支付宝！','error');
	}
	/*
		打款到银行账户
	*/
	protected function pay_balance_by_union($mid,$money,$balancesn){
		exi('暂不支持打款到银行账户！','error');
	}
}